﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DeploymentMaxLib.Actions
{
    public class ActionBuildVB6 : ActionBase
    {
        /// <summary>
        /// 
        /// </summary>
        public override string LongName
        {
            get { return Common.Constants.LongName; }
        }

        public override string Description
        {
            get { return Common.Constants.Description; }
        }

        public override ActionResponse Execute(ActionRequest pActionRequest)
        {
            
            base.Result = Validate(pActionRequest);

            if (!base.Result.HasErrorOrCritical())
            {
                var tExecutionType = pActionRequest.ExecutionType.ToEnum<Common.ExecutionType>();

                switch (tExecutionType)
                {
                    case Common.ExecutionType.Build:
                        {
                            Build(pActionRequest);
                        } break;
                }
            }

            return pActionRequest.GetResponse(this); 
        }

        public void Build(ActionRequest pActionRequest)
        {
            var tFileName = pActionRequest.Params.Where(t => t.Name == "FileName").First().Value;

            var tLogFile = string.Format(".\\{0}.log", System.IO.Path.GetFileNameWithoutExtension(tFileName));

            base.AddLog("LOG FILE NAME: {0}", tLogFile, ActionLogLevelType.Low);

            var tCommand = string.Format(Common.Constants.VB6BuildCommand, Common.Constants.VB6ExeLocation, tFileName, tLogFile);

            base.AddLog("BUILD COMMAND: {0}", tCommand, ActionLogLevelType.Low);

            if (System.IO.File.Exists(tLogFile)) System.IO.File.Delete(tLogFile);

            try
            {
                ///1. delete logfile if it exists
                ///2. execute command
                ///3. verify build successful

                var tErrorLog = "";
                var tOutLog = "";

                base.Result.AddResult(DeploymentMaxLib.Common.ExecuteBatch(tCommand, true, out tErrorLog, out tOutLog));

                using (var sr = new System.IO.StreamReader(tLogFile))
                {
                    while (!sr.EndOfStream)
                    {
                        var line = sr.ReadLine();
                        if (!line.IsEmpty())
                        {
                            if (System.Text.RegularExpressions.Regex.IsMatch(line, Common.Constants.VB6BuildSuccessfullRegEx))
                            {
                                base.Result.AddResult(new Exception(line));
                            }
                            break;
                        }
                    }
                    sr.Close();
                }

                System.IO.File.Delete(tLogFile);

            }
            catch (Exception exc)
            {
                base.Result.AddResult(exc);
            }

            //TODO: parse log and add to Log object


        }

        public override ActionResult Validate(ActionRequest pActionRequest)
        {
            var tResult = new ActionResult();

            return tResult;
        }
 
        public static class Common
        {
            public enum ExecutionType
            {
                None = 0,
                Build = 1,
            }

            public static class Constants
            {
                public const string LongName = "VB6 Build";
                public const string Description = "Handles VB6 Build of projects.";
                public const string VB6ExeLocation = @"C:\Program Files\Microsoft Visual Studio\VB98\vb6.exe";
                public const string VB6BuildCommand = "{0} /m \"{1}\" /out \"{2}\"";
                public const string VB6BuildSuccessfullRegEx = "Build of '.*' succeeded.";
            }

            
        }
    }
}
